package cn.usian.order.service.impl;

import cn.usian.comon.iterceptor.AutoThreadLocal;
import cn.usian.feign.CartFeign;
import cn.usian.jyzx.model.entity.h5.CartInfo;
import cn.usian.jyzx.model.entity.order.OrderItem;
import cn.usian.jyzx.model.vo.common.Result;
import cn.usian.jyzx.model.vo.h5.TradeVo;
import cn.usian.order.mapper.OrderMapper;
import cn.usian.order.service.OrderService;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    CartFeign cartFeign;
    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @Override
    public TradeVo trade() {
//        Long userid = AutoThreadLocal.getThreadLocal().getId();
//        List values = redisTemplate.opsForHash().values("jyzx" + userid);

        Result<List<CartInfo>> listResult = cartFeign.carList();
        List<CartInfo> cartInfoList = listResult.getData();
        BigDecimal bigDecimal = new BigDecimal(0);
        List<OrderItem> orderItems = new ArrayList<>();
        for (CartInfo cartInfo : cartInfoList) {
//            金额总计累加
            bigDecimal= bigDecimal.add(cartInfo.getCartPrice().multiply(BigDecimal.valueOf(cartInfo.getSkuNum())));
//            创建订单项
            OrderItem orderItem = new OrderItem();
//            赋值到orderItem
            BeanUtils.copyProperties(cartInfo,orderItem);
            orderItem.setSkuPrice(cartInfo.getCartPrice());
            orderItem.setThumbImg(cartInfo.getImgUrl());
            orderItems.add(orderItem);
        }
        TradeVo tradeVo = new TradeVo();
        tradeVo.setTotalAmount(bigDecimal);
        tradeVo.setOrderItemList(orderItems);
        return tradeVo;
    }
}
